"ui";
(function() {
    //继承至ui.Widget
    util.extend(MagicIndicator, ui.Widget);

    function MagicIndicator() {
        ui.Widget.call(this);
        let labelPaint = new com.stardust.autojs.core.graphics.Paint();
        let labelRect = new android.graphics.RectF()
        labelPaint.setAntiAlias(true);
        labelPaint.setStyle(android.graphics.Paint.Style.FILL);
        labelRect.set(0, 0, 0, 0);
        let centerVH = 0
        let tipheight = 0
        let tipwidth = 0
        let mTabs=[]
        let currentTab=0;
        let mPager=null
        
        let bgColor = colors.parseColor("#00ffffff")
        let mTextColorNormal = colors.parseColor("#88000000")
        let mTextColorLight = colors.parseColor("#ff000000")
        let mCursorColor = colors.parseColor("#55000000")
       
        let scope = this


        this.defineAttr("w", (view, attr, value, defineSetter) => {
            view.attr("w", value)
        });
        this.defineAttr("h", (view, attr, value, defineSetter) => {
            view.attr("h", value)
        });
        
        this.defineAttr("backgroundColor", (view, attr, value, defineSetter) => {
            bgColor = colors.parseColor(value)
        })
        this.defineAttr("textColorLight", (view, attr, value, defineSetter) => {
            mTextColorLight = colors.parseColor(value)
        })
        this.defineAttr("textColorNormal", (view, attr, value, defineSetter) => {
            mTextColorNormal = colors.parseColor(value)
        })
        this.defineAttr("cursorColor", (view, attr, value, defineSetter) => {
            mCursorColor = colors.parseColor(value)
        })
        this.defineAttr("id", (view, attr, value, defineSetter) => {
            ui[value.replace("@+id/", "")] = view.getChildAt(0)
        });


        this.onViewCreated = function(view) {
              
        }
        this.onFinishInflation=function(view){
            view.setHorizontalScrollBarEnabled(false);
            view.setOverScrollMode( android.view.View.OVER_SCROLL_NEVER );
            defineMethod.call(this,view.getChildAt(0))     
        }
        function defineMethod(view) {
            view.setTabs = function(tabs) {             
                var bgdb = new android.graphics.drawable.Drawable({
                    draw: function(canvas) {                       
                        canvas.drawARGB(colors.alpha(bgColor),colors.red(bgColor),colors.green(bgColor),colors.blue(bgColor))
                        labelPaint.setColor(mCursorColor);
                        canvas.drawRoundRect(labelRect, tipheight / 2, tipheight / 2, labelPaint)
                    }
                })
                view.setBackgroundDrawable(bgdb)
                view.post(function() {
                    if (scope.view.getChildCount() == 0) return;
                    let tabV = view.getChildAt(0);
                    tipheight = tabV.getMeasuredHeight();
                    tipwidth = tabV.getMeasuredWidth();
                    centerVH = view.getHeight() / 2;
                    labelRect.set(tabV.getLeft(), centerVH - tipheight / 2, tabV.getRight(), centerVH + tipheight / 2);
                    view.invalidate()
                })

                view.removeAllViews()
                
                mTabs = tabs
                currentTab = 0
                for (let i = 0; i < tabs.length; i++) {
                    let tab = tabs[i]
                    
                    let item = ui.inflate(
                        <text id="_title" padding="8 4 8 4" textSize="16" layout_gravity="center" gravity="center"/>, view)                   
                    item.setTextColor(mTextColorNormal)
                    item.setTag(i)
                    item._title.setText(tab)
                    item.click((view) => {
                        mPager.setCurrentItem(view.getTag())
                    })
                    
                    view.addView(item)
                }
                setTabLight(0)
            };
            view.setViewPager = function(pager) {
                mPager = pager
                pager.setOnPageChangeListener({
                    onPageScrolled: function(arg0I, arg1F, arg2F) {
                        scroll(arg0I, arg1F)
                    },
                    onPageSelected: function(arg0I) {
                        setTabLight(arg0I)
                    }
                })
            }

            function setTabLight(arg0) {
                let count = view.getChildCount();
                for (let i = 0; i < count; i++) {
                    let cView = view.getChildAt(i);
                    
                    if (i == arg0) {
                        cView.setTextColor(mTextColorLight);
                    } else {
                        cView.setTextColor(mTextColorNormal);
                    }
                }
            }
            let lastPos=0;
            let lastOffset=0;
            function scroll(position, offset) {
                let direct=position>lastPos||(position==lastPos&&offset>lastOffset)?1:-1
                lastOffset=offset;
                lastPos=position;
                
                let list =view
                let sc=list.getParent()
                if (position == mTabs.length - 1) {
                    labelRect.set(list.getChildAt(position).getLeft(), centerVH - tipheight / 2, list.getChildAt(position).getRight(), centerVH + tipheight / 2);
                    
                } else {
                    let l = (1 - offset) * list.getChildAt(position).getLeft() + offset * list.getChildAt(position + 1).getLeft();
                    tipwidth = (1 - offset) * list.getChildAt(position).getWidth() + offset * list.getChildAt(position + 1).getWidth()
                    labelRect.set(l, centerVH - tipheight / 2, l + tipwidth, centerVH + tipheight / 2);
                }
                view.invalidate()
            }
        }
    }

    MagicIndicator.prototype.render = function() {
        return (
            <HorizontalScrollView>
                <linear id="inner" paddingLeft="8"/>
            </HorizontalScrollView>
        );
    }
    ui.registerWidget("MagicIndicator", MagicIndicator);
    return MagicIndicator
})();

ui.statusBarColor("#303030")
ui.layout(
    <vertical>
        <MagicIndicator backgroundColor="#303030" cursorColor="#66ffffff" textColorLight="#ffffff" textColorNormal="#66ffffff" id="mi" w="*" h="45"/>
        <viewpager id="pager">
            <text text="145" id="t1" textSize="16" bg="#FFCADDC7"/>
            <text text="123" textSize="16" bg="#456789"/>
            <text text="321" textSize="16" bg="#987654"/>
            <text text="hjhjjj" textSize="16" bg="#ffaa66"/>
        </viewpager>
    </vertical>
)

ui.mi.setTabs(["短", "测试短", "测试长ABCDEFG","abcdefghijklmnopqrstuvwxyz"])
ui.mi.setViewPager(ui.pager)
